package com.senlingspace.web.controller.headquarters;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.Lists;
import com.senlingspace.common.constant.HttpStatus;
import com.senlingspace.system.domain.SlkjFareSchemeShop;
import com.senlingspace.system.domain.vo.FareSchemeQueryVo;
import com.senlingspace.system.domain.dto.FareSchemeRespDto;
import com.senlingspace.system.domain.vo.FareSchemeRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.senlingspace.common.annotation.Log;
import com.senlingspace.common.core.controller.BaseController;
import com.senlingspace.common.core.domain.AjaxResult;
import com.senlingspace.common.enums.BusinessType;
import com.senlingspace.system.service.ISlkjFareSchemeService;
import com.senlingspace.common.utils.poi.ExcelUtil;
import com.senlingspace.common.core.page.TableDataInfo;

/**
 * 票价方案Controller
 *
 * @author SenlingSpace
 * @date 2024-07-15
 */
@RestController
@RequestMapping("/system/scheme")
public class SlkjFareSchemeController extends BaseController {
    @Autowired
    private ISlkjFareSchemeService slkjFareSchemeService;


    /**
     * 查询票价方案列表
     */
    @PreAuthorize("@ss.hasPermi('system:scheme:list')")
    @GetMapping("/list")
    public TableDataInfo list(FareSchemeQueryVo fareSchemeQuery) {
        int code = slkjFareSchemeService.existsTicketLevelAndTicketCategory();
        if (code != HttpStatus.SUCCESS) {
            TableDataInfo rspData = new TableDataInfo();
            rspData.setCode(code);
            rspData.setMsg("需要先创建票档/票类，才可以做票价方案！");
            rspData.setRows(Lists.newArrayList());
            rspData.setTotal(0);
            return rspData;
        }

        startPage();
        List<FareSchemeRespDto> list = slkjFareSchemeService.selectSlkjFareSchemeList(fareSchemeQuery);
        return getDataTable(list);
    }

    /**
     * 导出票价方案列表
     */
    @PreAuthorize("@ss.hasPermi('system:scheme:export')")
    @Log(title = "票价方案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FareSchemeQueryVo fareSchemeQuery) {
        List<FareSchemeRespDto> list = slkjFareSchemeService.selectSlkjFareSchemeList(fareSchemeQuery);
        ExcelUtil<FareSchemeRespDto> util = new ExcelUtil<>(FareSchemeRespDto.class);
        util.exportExcel(response, list, "票价方案数据");
    }

    /**
     * 获取票价方案详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:scheme:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return slkjFareSchemeService.selectSlkjFareSchemeById(id);
    }

    /**
     * 新增票价方案
     */
    @PreAuthorize("@ss.hasPermi('system:scheme:add')")
    @Log(title = "票价方案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FareSchemeRequest fareSchemeRequest) {
        return slkjFareSchemeService.insertSlkjFareScheme(fareSchemeRequest, super.getUserId());
    }

    /**
     * 修改票价方案
     */
    @PreAuthorize("@ss.hasPermi('system:scheme:edit')")
    @Log(title = "票价方案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FareSchemeRequest request) {
        return slkjFareSchemeService.updateSlkjFareScheme(request, super.getUserId());
    }

    /**
     * 删除票价方案
     */
    @PreAuthorize("@ss.hasPermi('system:scheme:remove')")
    @Log(title = "票价方案", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return slkjFareSchemeService.deleteSlkjFareSchemeByIds(ids, super.getUserId());
    }

    /**
     * 新建票价方案的时候，页面获取票档和票类的数据
     *
     * @return {@link AjaxResult }
     */
    @GetMapping("/getTicketLevelCategory")
    public AjaxResult getTicketLevelCategory() {
        return slkjFareSchemeService.getTicketLevelCategory();
    }

    /**
     * 门店管理-票价管理列表
     *
     * @param id 方案 ID
     * @return {@link AjaxResult }
     */
    @GetMapping("/fareDetail")
    public AjaxResult fareDetail(@RequestParam Long id) {
        return slkjFareSchemeService.selectSlkjFareSchemeById(id);
    }

    /**
     * 新增门店票价方案
     *
     * @param fareSchemeShop
     * @return {@link AjaxResult }
     */
    @PostMapping("/shopSaveFareScheme")
    public AjaxResult shopSaveFareScheme(@RequestBody SlkjFareSchemeShop fareSchemeShop) {
        if (fareSchemeShop.getFareSchemeId() == null) {
            return AjaxResult.error("票价方案ID不能为空！");
        }

        if (fareSchemeShop.getShopId() == null) {
            return AjaxResult.error("门店ID不能为空！");
        }

        return slkjFareSchemeService.shopSaveFareScheme(fareSchemeShop);
    }

    /**
     * 票价方案下拉框列表
     *
     * @return {@link AjaxResult }
     */
    @GetMapping("/schemeSelected")
    public AjaxResult schemeSelected() {
        return slkjFareSchemeService.schemeSelected();
    }

}
